﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using Project1.Interface;
using Project1.Common;
using HalconDotNet;
using Newtonsoft.Json;
using System.Windows;
namespace Project1.Element
{
    public class MeasurePlan
    {


        private static int planNum = 0;
        private string planName;
        //优先级 ieps -> icms
        List<IElemPack> ieps;//单个元素的检测任务
        List<IComplexMes> icms;//测量任务

        public string PlanName
        {
            get { return planName; }
            set { planName = value; }
        }
        public List<IElemPack> IEPS { get { return ieps; } set { ieps = value; } }
        public List<IComplexMes> ICMS { get { return icms; }  set { icms = value; } }
        public MeasurePlan()
        {
            planName = "MeasurePlan" + planNum++;
            ieps = new List<IElemPack>();
            icms = new List<IComplexMes>();
        }
        public void Add(IComplexMes icm)
        {
            if (icm == null) throw new Exception();
            icms.Add(icm);
        }

        public void Add(IElemPack iep)
        {
            if (iep == null) throw new Exception();
            ieps.Add(iep);
        }

        public void Execute(HObject image, HTuple homMat2D,HTuple reVerseHomMat2D,AddToList addToList, AddSupline addSupline,FindMethod findMethod)
        {
        
            foreach (var iep in ieps)
            {
                if(iep.DetectMethod == ConstValue.MEASURE_POINT)
                {
                    PointPack p = (PointPack)iep;
                    p.Detect(image, homMat2D);
                    addToList(p, ConstValue.POINT);
                }
                else if(iep.DetectMethod == ConstValue.MEASURE_LINE)
                {
                    LinePack l = (LinePack)iep;
                    l.Detect(image, homMat2D);
                    addToList(l, ConstValue.LINE);
                }
                else if (iep.DetectMethod == ConstValue.MEASURE_CIRCLE)
                {
                    CirclePack c = (CirclePack)iep;
                    c.Detect(image, homMat2D);
                    addToList(c, ConstValue.CIRCLE);
                }
            }
            foreach (var icm in icms)
            {
                icm.Execute(image, homMat2D, reVerseHomMat2D, addToList, findMethod, addSupline);
            }
        }


        /// <summary>
        /// 保存规划到本地
        /// </summary>
        /// <param name="path"></param>
        public void SaveToLocal(string path)
        {
            //IElemPack iep = new PointPack();
            //string jsonData1 = JsonConvert.SerializeObject(iep);
            //string jsonData = JsonConvert.SerializeObject(this);
            using (StreamWriter file = File.CreateText(path+@"\"+planName+".json")) // path = @"c:\movie.json"
            {
                //JsonSerializer serializer = new JsonSerializer();
                var settings = new JsonSerializerSettings()
                {
                    TypeNameHandling = TypeNameHandling.All
                };
                string jsonData = JsonConvert.SerializeObject(this, settings);
                file.BaseStream.Seek(0, SeekOrigin.End);
                file.Write(jsonData);
            
                MessageBox.Show("保存成功", "结果", MessageBoxButton.OK);


                // MeasurePlan mp = JsonConvert.DeserializeObject<MeasurePlan>(this, settings);
                // serializer.TypeNameHandling = TypeNameHandling.All;
                // serializer.Serialize(file, this);

                //Console.WriteLine(jsonData);
                //Console.ReadLine();
            }
       
        }

        /// <summary>
        /// 从本地读取任务规划
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public  static  List<MeasurePlan> LoadMeasure(string path)
        {
            var settings = new JsonSerializerSettings()
            {
                TypeNameHandling = TypeNameHandling.All
            };
            List<MeasurePlan> measurePlans = new List<MeasurePlan>();
            List<string> paths = new List<string>();
            paths.Add(path);
            var rs = CheckFileEx(paths, "json");
            foreach (var name in rs)
            {
                //MessageBox.Show(name);
                string jsonData = File.ReadAllText(name);
                MeasurePlan mp = JsonConvert.DeserializeObject<MeasurePlan>(jsonData, settings);
                measurePlans.Add(mp);
            }
            return measurePlans;
        }







        /// <summary>
        /// 获取指定类型的所有文件
        /// </summary>
        /// <param name="paths">文件列表</param>
        /// <param name="ex">保留的 格式，多个以逗号隔开(.txt,.jpg)</param>
        /// <returns></returns>
        private  static string[] CheckFileEx(List<string>paths, string ex)
        {
            List<string> existedFiles = new List<string>();
            foreach (var path in paths)
            {
                DirectoryInfo di = new DirectoryInfo(path);
                FileInfo[] files = di.GetFiles();
                foreach (var item in files)
                {
                    if (item.Extension.ToLower() == "."+ex)
                    {
                        existedFiles.Add(item.FullName);
                    }
                }
            }
            return existedFiles.ToArray();

        }



    }
}
